library NeutralExtraSpawnControl initializer init requires LegacySystem

	globals
		private trigger T
	endglobals

	private function TheCloud takes nothing returns nothing
		local unit u
		local integer i = 1
		local integer i2 = GetRandomInt(2,12)
		local integer i3 = udg_Sorted[GetRandomInt(1,udg_Players)]
		local real x1 = GetUnitX(udg_Hero[i3])
		local real y1 = GetUnitY(udg_Hero[i3])
		local location l2 = SelectPoint2(x1,y1,1200,2500)
		local real x2 = GetLocationX(l2)
		local real y2 = GetLocationY(l2)
		set udg_TheCloud = true
		if udg_SectorClear or udg_CurrentWave==5 then
			call RemoveLocation(l2)
			set l2=null
			return
		endif
		set u = CreateUnit(Player(14),'U00M',x2,y2,0.0)
		call SuspendHeroXP(u,true)
		if udg_Mode==2 then
			call GeneralText(10.0, "|cfff3ad00Major Grimes|r : We've spotted an unknown phenomena at coordinates ( " + I2S(R2I(x2)) + ", " + I2S(R2I(y2)) + " )." )
		endif
		call RemoveLocation(l2)
		set l2 = null
		loop
			exitwhen i>i2
			if GetWidgetLife(u)>0.405 then
				call PingMinimapEx(GetUnitX(u),GetUnitY(u),2.5,125,100,200,false)
				call StartSound(gg_snd_HeartBeat)
				call PolledWait(8.)
			else
				exitwhen 1==1
			endif
			set i=i+1
		endloop
		if GetWidgetLife(u)>0.405 then
			call KillUnit(u)
		endif
		if u!=null then
			set u = null
		endif
		set udg_TheCloud = false
	endfunction

	private function AIGray takes nothing returns nothing
		local group g = CreateGroup()
		local group g2 = CreateGroup()
		local unit u
		local real x
		local real y
		local integer i = 1
		call GroupEnumUnitsOfPlayer(g, Player(14), null)
		loop
			exitwhen i>udg_Players
			if IsUnitVisible(udg_Hero[udg_Sorted[i]],Player(14)) then
				call GroupAddUnit(g2,udg_Hero[udg_Sorted[i]])
			endif
			set i = i + 1
		endloop
		set u = GroupPickRandomUnit(g2)
		if u==null then
			set u = udg_Hero[udg_Sorted[GetRandomInt(1,udg_Players)]]
			//if u==udg_Marksman and GetUnitAbilityLevel(udg_Marksman,'A016')!=0 and udg_Players>1 then
			//    set u = udg_Hero[udg_Sorted[GetRandomInt(1,udg_Players)]]
			//endif
		endif
		if GetWidgetLife(u)<=0.405 then
			set x = GetRandomReal(-7000.0,11000.0)
			set y = GetRandomReal(-4000.0,14000.0)
		else
			set x = GetUnitX(u)
			set y = GetUnitY(u)
		endif
		call DestroyGroup(g2)
		set g2 = null
		loop
			set u = FirstOfGroup(g)
			exitwhen u==null
			call IssuePointOrder(u,"attack",x,y)
			call GroupRemoveUnit(g,u)
		endloop
		call DestroyGroup(g)
		set g = null
		set i = 1

		if GetRandomInt(1,65)==1 then
			call StartSound(udg_ZombieAmbience[GetRandomInt(1,6)])
		elseif GetRandomInt(1,udg_CloudFactor)==1 and udg_TheCloud==false and ((udg_CurrentWave>2 and udg_SquadXP>5000) or udg_Mode==3) then
			call TheCloud()
		elseif GetRandomInt(1,200)==1 and udg_Intermission==false and udg_SectorClear==false then
			call SatelliteCamera.execute()
		endif
	endfunction

	function SetAINeutralExtraEnabled takes boolean b returns nothing
		if b then
			call EnableTrigger(T)
		else
			call DisableTrigger(T)
		endif
	endfunction
	
	private function init takes nothing returns nothing
		set T = CreateTrigger()
        call TriggerRegisterTimerEvent(T, 6.2, true)
		call DisableTrigger(T)
		call TriggerAddAction(T,function AIGray)
	endfunction
	
endlibrary
